﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.DAL;

namespace BOMS.WebUI
{
    public partial class sysDepartmentTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //string userId = HttpContext.Current.User.Identity.Name;
                string userId = Request.QueryString["userId"].ToString();
                bind(userId);
            }
        }
       
        private void bind(string userId)
        {
            
            BLL.M_SubLans bll = new BLL.M_SubLans();
            DataTable rootTbl = bll.GetOrgByParentID("");
            if (rootTbl.Rows.Count > 0)
            {
                string RootName = rootTbl.Rows[0]["ShortName"].ToString();
                string RootID = rootTbl.Rows[0]["OrgID"].ToString();
                TreeNode Root = new TreeNode(RootName, RootID);
                Root.PopulateOnDemand = false;
                //绑定已选部门
                string[] orgIDArray = null; ;
                if (userId != "0")
                {
                    DataTable dtUserInfo = SysAdmin.getUserInfoById(userId);
                    string orgIDs = dtUserInfo.Rows[0]["orgIdArray"].ToString();
                    orgIDArray = orgIDs.Split(','); 
                    foreach (string orgId in orgIDArray)
                    {
                        if (Root.Value == orgId)
                            Root.Checked = true;
                    }
                }
                tvOrg.Nodes.Add(Root);
                DataTable partCorpTbl = bll.GetOrgByParentID(RootID);
                for (int i = 0; i < partCorpTbl.Rows.Count; i++)
                {
                    TreeNode PartCorp = new TreeNode(partCorpTbl.Rows[i]["ShortName"].ToString(), partCorpTbl.Rows[i]["OrgID"].ToString());
                    PartCorp.PopulateOnDemand = true;
                    PartCorp.SelectAction = TreeNodeSelectAction.Expand;
                    Root.ChildNodes.Add(PartCorp);
                    //绑定已选部门
                    if (userId != "0")
                    {
                        foreach (string orgId in orgIDArray)
                        {
                            if (PartCorp.Value == orgId)
                                PartCorp.Checked = true;
                        }
                    }
                }
            }
        }

        protected void tvOrg_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            BLL.M_SubLans bll = new BLL.M_SubLans();
            string PID = e.Node.Value;
            DataTable batteryRoomTbl = bll.GetOrgByParentID(PID);
            for (int i = 0; i < batteryRoomTbl.Rows.Count; i++)
            {
                TreeNode BatteryRoom = new TreeNode();
                BatteryRoom.Text = batteryRoomTbl.Rows[i]["ShortName"].ToString();
                BatteryRoom.Value = batteryRoomTbl.Rows[i]["OrgID"].ToString();
                BatteryRoom.PopulateOnDemand = false;
                e.Node.ChildNodes.Add(BatteryRoom);
                //绑定已选部门
                string userId = Request.QueryString["userId"].ToString();
                if (userId != "0")
                {
                    DataTable dtUserInfo = SysAdmin.getUserInfoById(userId);
                    string orgIDs = dtUserInfo.Rows[0]["orgIdArray"].ToString();
                    string[] orgIDArray = orgIDs.Split(',');
                    foreach (string orgId in orgIDArray)
                    {
                        if (BatteryRoom.Value == orgId)
                            BatteryRoom.Checked = true;
                    }
                }
            }
        }

        protected void add_Click(object sender, EventArgs e)
        {
            string orgIdArr = "";
            string orgNameArr = "";
            foreach (TreeNode rootNode in tvOrg.Nodes)
            {

                if (rootNode.Checked == true)
                {
                    orgIdArr = orgIdArr + rootNode.Value + ",";
                    orgNameArr = orgNameArr + rootNode.Text + ",";
                }
                foreach (TreeNode firstNode in rootNode.ChildNodes)
                {
                    if (firstNode.Checked == true)
                    {
                        orgIdArr = orgIdArr + firstNode.Value + ",";
                        orgNameArr = orgNameArr + firstNode.Text + ",";
                    }
                    foreach (TreeNode secondNode in firstNode.ChildNodes)
                    {
                        if (secondNode.Checked == true)
                        {
                            orgIdArr = orgIdArr + secondNode.Value + ",";
                            orgNameArr = orgNameArr + secondNode.Text + ",";
                        }

                    }
                }

            }
            Type cstype = this.GetType();
           // ClientScript.RegisterStartupScript(cstype, "key", "orgDialogReturn('abc');", true);
            ClientScript.RegisterStartupScript(cstype, "key", "orgDialogReturn('" + orgIdArr + "','" + orgNameArr + "');", true);
        }
    }
}